var grid;
$(document).ready(function() {
	/////////////jehc扩展属性目的可方便使用（boot.js文件datatablesCallBack方法使用） 如弹窗分页查找根据条件 可能此时的form发生变化 此时 可以解决该类问题
	var opt = {
		searchformId:'searchForm'
	};
	var options = DataTablesPaging.pagingOptions({
		ajax:function (data, callback, settings){datatablesCallBack(data, callback, settings,workflowModules+'/lcProcess/list',opt);},//渲染数据
			//在第一位置追加序列号
			fnRowCallback:function(nRow, aData, iDisplayIndex){
				jQuery('td:eq(1)', nRow).html(iDisplayIndex +1);  
				return nRow;
		},
		order:[],//取消默认排序查询,否则复选框一列会出现小箭头
        tableHeight:datatablesDefaultHeight2,
        sPaginationType:'simple_numbers',
        fixedHeader:true,//表头固定
        fixedColumns: {
            "leftColumns": 4,
            "rightColumns": 1
        },
		//列表表头字段
		colums:[
			{
				sClass:"text-center",
				width:"50px",
				data:"lc_process_id",
				render:function (data, type, full, meta) {
					return '<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline"><input type="checkbox" name="checkId" class="checkchild " value="' + data + '" /><span></span></label>';
				},
				bSortable:false
			},
			{
				data:"lc_process_id"
			},
            {
                data:'lc_process_title'
            },
            {
                data:"lc_process_id",
                render:function(data, type, row, meta) {
                    var lc_process_flag = row.lc_process_flag;
                    var lc_process_title = row.lc_process_title;
                    var xt_attachment = row.xt_attachment;
                    var btn = "";
                    if(lc_process_flag == 0) {
                        // btn = '<div class="btn-group ml-2">' +
                        //         '<a href="#" class="btn btn-light-primary font-weight-bold" data-toggle="dropdown" aria-expanded="true">' +
                        //             '<i class="fa flaticon-truck"></i>选项' +
                        //         '</a>' +
                        //         '<button type="button" class="btn btn-light-primary font-weight-bold dropdown-toggle dropdown-toggle-split mr-1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'+
                        //             '<span class="sr-only"></span>'+
                        //         '</button>'+
                        //         '<div class="dropdown-menu">' +
                        //             '<button class="dropdown-item" onclick=addLcDesign("' + data + '","' + lc_process_title + '")><i class="fa flaticon-logout"></i>设计流程</button>' +
                        //             '<button class="dropdown-item" onclick=downFileBpmn("' + data + '")><i class="fa flaticon-download"></i>下载bpmn文件</a>' +
                        //             '<button class="dropdown-item" onclick=downFileImg("' + data + '")><i class="fa flaticon-attachment"></i>下载img文件</a>' +
                        //             '<button class="dropdown-item" onclick=showProcessImage("' + data + '")><i class="la la-file-image-o"></i>流程图</a>' +
                        //             '<button class="dropdown-item" onclick=toLcProcessDetail("' + data + '")><i class="fa flaticon-list"></i>详情</a>' +
                        //         '</div>' +
                        //     '</div>';
                        btn = '<button class="btn btn-link" onclick=addLcDesign("' + data + '","' + lc_process_title + '")>设计流程</button>' +
                            // '<button class="btn btn-link" onclick=downFileBpmn("' + data + '")>下载bpm</a>' +
                            // '<button class="btn btn-link" onclick=downFileImg("' + data + '")>下载img</a>' +
                            '<button class="btn btn-link" onclick=showProcessImage("' + data + '")>监 控</a>' +
                            '<button class="btn btn-link" onclick=toLcProcessDetail("' + data + '")>详 情</a>';
                    }else{
                        // btn = '<div class="btn-group ml-2">' +
                        //         '<a href="#" class="btn btn-light-primary font-weight-bold" data-toggle="dropdown" aria-expanded="true">' +
                        //             '<i class="fa flaticon-truck"></i>选项' +
                        //         '</a>' +
                        //         '<button type="button" class="btn btn-light-primary font-weight-bold dropdown-toggle dropdown-toggle-split mr-1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'+
                        //             '<span class="sr-only"></span>'+
                        //         '</button>'+
                        //         '<div class="dropdown-menu dropdown-menu-right">' +
                        //             '<button class="dropdown-item" onclick=downFile("' + xt_attachment + '")><i class="fa flaticon-attachment"></i>下载附件</a>' +
                        //             '<button class="dropdown-item" onclick=toLcProcessDetail("' + data + '")><i class="fa flaticon-list"></i>详情</a>' +
                        //         '</div>' +
                        //     '</div>';
                        btn = '<button class="btn btn-link" onclick=downFile("' + xt_attachment + '")>下载附件</a>' +
                            '<button class="btn btn-link" onclick=toLcProcessDetail("' + data + '")>详 情</a>';
                    }
                    return btn+
                        // '<button onclick=toLcProcessDetail("'+data+'") class="btn btn-light-primary font-weight-bold mr-1" title="详 情">' +
                        // '<i class="fa flaticon-list"></i>详 情' +
                        // '</button>'+
                        '</button>'+
                        '<button onclick=copyProcess("'+data+'") class="btn btn-link" title="复制一个模型">' +
                        '复制流程' +
                        '</button>'+
                        '<button onclick=showLcDeploymentHis("'+data+'","'+lc_process_title+'") class="btn btn-link" title="发布版本记录">' +
                        '版 本</button>'+
                        '<button onclick=createDeployment("'+data+'") class="btn btn-link" title="发布流程">' +
                        '发 布</button>';
                }
            },
			{
				data:'lc_process_status',
                render:function(data, type, row, meta) {
                    if(data == 1){
                        return "<span class='label label-lg font-weight-bold label-light-success label-inline'>发布中</span>"
                    }
                    if(data == 0){
                        return "<span class='label label-lg font-weight-bold label-light-info label-inline'>待发布</span>"
                    }
                    if(data == 2){
                        return "<span class='label label-lg font-weight-bold label-light-danger label-inline'>已关闭</span>"
                    }
                    return "<span class='label label-lg font-weight-bold label-light-info label-inline'>缺省</span>"
                }
			},
            {
                data:'del_flag',
                render:function(data, type, row, meta) {
                    if(data == 0){
                        return "<span class='label label-lg font-weight-bold label-light-success label-inline'>使用中</span>"
                    }
                    if(data == 1){
                        return "<span class='label label-lg font-weight-bold label-light-danger label-inline'>已废弃</span>"
                    }
                    return "<span class='label label-lg font-weight-bold label-light-info label-inline'>缺省</span>"
                }
            },
			{
				data:'lc_process_flag',
                render:function(data, type, row, meta) {
                    if(data == 0){
                        return "<span class='label label-lg font-weight-bold label-light-primary label-inline'>通过平台设计器设计</span>"
                    }
                    if(data == 1){
                        return "<span class='label label-lg font-weight-bold label-light-info label-inline'>通过上传部署</span>"
                    }
                    return "<span class='label label-lg font-weight-boldlabel-light-success label-inline'>缺省</span>"
                }
			},
            {
                data:'version',
                render:function(data, type, row, meta) {
                    return "<span class='label label-light-warning mr-2'>"+data+"</span>"
                }
            },
            {
                data:"name"
            },
            {
                data:"groupName"
            },
            {
                data:'w',
                render:function(data, type, row, meta) {
                    return "<span class='label label-light-info mr-2'>"+data+"</span>"
                }
            },
            {
                data:'h',
                render:function(data, type, row, meta) {
                    return "<span class='label label-light-info mr-2'>"+data+"</span>"
                }
            },
            {
                data:"createBy",
                render:function(data, type, row, meta) {
                    return data;
                }
            },
            {
                data:"create_time",
                render:function(data, type, row, meta) {
                    return dateformat(data);
                }
            },
            {
                data:"modifiedBy",
                render:function(data, type, row, meta) {
                    return data;
                }
            },
            {
                data:"update_time",
                render:function(data, type, row, meta) {
                    return dateformat(data);
                }
            },
            {
                data:"moduleKey"
            }
		]
	});
	grid=$('#datatables').dataTable(options);
	//实现全选反选
	docheckboxall('checkall','checkchild');
	//实现单击行选中
	clickrowselected('datatables');
	//产品线下拉框数据
    initProductList("lc_product_id");
    // $("#datatables").colResizable();
});
//新增
function toLcProcessAdd(){
	tlocation(base_html_redirect+'/workflow/lc-process/lc-process-add.html');
}
//修改
function toLcProcessUpdate(){
	if($(".checkchild:checked").length != 1){
		toastrBoot(4,"选择数据非法");
		return;
	}
	var id = $(".checkchild:checked").val();
	var table = $('#datatables').dataTable();
	var List = getTableContent(table);
	for(var i = 0; i < List.length; i++){
		var obj = List[i];
		if(obj.lc_process_id == id && obj.lc_process_flag == 0){
			toastrBoot(4,"通过平台设计器设计的数据不能在编辑中修改 只能在设计器中修改");
			return;
		}
	}
    tlocation(base_html_redirect+'/workflow/lc-process/lc-process-update.html?lc_process_id='+id);
}
//详情
function toLcProcessDetail(id){
    tlocation(base_html_redirect+'/workflow/lc-process/lc-process-detail.html?lc_process_id='+id);
}
//删除
function delLcProcess(){
	if(returncheckedLength('checkchild') <= 0){
		toastrBoot(4,"请选择要废弃的数据");
		return;
	}
	msgTishCallFnBoot("确定要废弃所选择的数据？",function(){
		var id = returncheckIds('checkId').join(",");
		var params = {lc_process_id:id};
		ajaxBReq(workflowModules+'/lcProcess/delete',params,['datatables'],null,"DELETE");
	});
}

/**
 *
 * @param lc_process_id
 * @param lc_process_title
 */
function addLcDesign(lc_process_id,lc_process_title){
    $('#lcDesignPanelBody').height(reGetBodyHeight()-128);
	$('#lcDesignModalLabel').html("在线设计---<font color=red>"+lc_process_title+"</font>");
	$('#lcDesignModal').modal({backdrop:'static',keyboard:false});
	$('#lcDesignModal').modal({"backdrop":"static"}).modal('show').on("shown.bs.modal",function(){
        $("#lcDesignIframe",document.body).attr("src",base_html_redirect+'/workflow/lc-design/lc-design2.0.html?lc_process_id='+lc_process_id);
        // 是弹出框居中。。。
        var $modal_dialog = $("#lcDesignModalDialog");
        $modal_dialog.css({'margin': 0 + 'px auto'});
        $modal_dialog.css({'width':reGetBodyWidth()+'px'});
    });
}

/**
 *
 */
function addDesign(){
    $('#lcDesignPanelBody').height(reGetBodyHeight()-128);
    // $('#lcDesignModalLabel').html("在线设计---<font color=red>"+lc_process_title+"</font>");
    $('#lcDesignModal').modal({backdrop:'static',keyboard:false});
    $('#lcDesignModal').modal({"backdrop":"static"}).modal('show').on("shown.bs.modal",function(){
        $("#lcDesignIframe",document.body).attr("src",base_html_redirect+'/workflow/lc-design/lc-design2.0.html');
        // 是弹出框居中。。。
        var $modal_dialog = $("#lcDesignModalDialog");
        $modal_dialog.css({'margin': 0 + 'px auto'});
        $modal_dialog.css({'width':reGetBodyWidth()+'px'});
    });
}

/**
 * 关闭窗体
 */
function closeLcProcessWin(){
	search('datatables');
}

/**
 * 版本
 * @param lc_process_id
 * @param lc_process_title
 */
function showLcDeploymentHis(lc_process_id,lc_process_title){
	$('#lcDeploymentHisPanelBody').height(reGetBodyHeight()-216);
	$('#lcDeploymentHisModalLabel').html("流程发布版本【<font color=red>"+lc_process_title+"</font>】");
	$('#lcDeploymentHisModal').modal({backdrop:'static',keyboard:false});
	$('#lcDeploymentHisModal').modal({"backdrop":"static"}).modal('show').on("shown.bs.modal",function(){
        var $modal_dialog = $("#lcDeploymentHisModalDialog");
        $("#lcDeploymentHisIframe",document.body).attr("src",base_html_redirect+'/workflow/lc-deployment-his/lc-deployment-his-list.html?lc_process_id='+lc_process_id);
        $modal_dialog.css({'margin': 0 + 'px auto'});
        $modal_dialog.css({'width':reGetBodyWidth()+'px'});
    });
}

/**
 * 下载图片
 * @param lc_process_id
 */
function downFileImg(lc_process_id){
	downOrExportB(workflowModules+'/lcProcess/downFileImg?lc_process_id='+lc_process_id);
}


/**
 * 下载附件
 * @param xt_attachment
 */
function downFile(xt_attachment){
	downOrExportB(workflowModules+'/xtCommon/downFile?xt_attachment_id='+xt_attachment);
}

/**
 * 下载BPMN文件
 * @param lc_process_id
 */
function downFileBpmn(lc_process_id){
	downOrExportB(workflowModules+'/lcProcess/downFileBpmn?lc_process_id='+lc_process_id);
}


/**
 * 发布流程
 * @param lc_process_id
 */
function createDeployment(lc_process_id){
	msgTishCallFnBoot("确定要发布该流程？",function(){
		var params = {lc_process_id:lc_process_id};
		ajaxBReq(workflowModules+'/lcProcess/createDeployment',params,['datatables'],null,"PUT");
	});
}


/**
 * 初始化产品线
 * @param id
 */
function initProductList(id){
    $("#"+id).html("");
    var str = "<option value=''>请选择</option>";
    $.ajax({
        type:"POST",
        url:workflowModules+"/lcProduct/find",
        success: function(result){
            result = result.data;
            //从服务器获取数据进行绑定
            $.each(result, function(i, item){
                str += "<option value=" + item.lc_product_id + ">" + item.name + "</option>";
            })
            $("#"+id).append(str);
        }
    });
}

/**
 *
 * @param id
 * @param value_id
 * @param lc_product_id
 */
function initGroupListSetV(id,value_id){
    $("#"+id).html("");
    var str = "<option value=''>请选择</option>";
    if(null != $("#"+value_id).val() && '' != $("#"+value_id).val()){
        $.ajax({
            type:"GET",
            url:workflowModules+"/lcGroup/find/"+$("#"+value_id).val(),
            success: function(result){
                result = result.data;
                //从服务器获取数据进行绑定
                $.each(result, function(i, item){
                    str += "<option value=" + item.lc_group_id + ">" + item.name + "</option>";
                })
                $("#"+id).append(str);
            }
        });
    }else{
        $("#"+id).append(str);
    }
}

/**
 * 流程图
 * @param id
 */
function showProcessImage(id) {
    var ImageModalCount = 0 ;
    $('#lcProcessImagePanelBody').height(reGetBodyHeight()-128);
    $('#lcProcessImageModal').modal({backdrop:'static',keyboard:false});
    $('#lcProcessImageModal').modal({"backdrop":"static"}).modal('show').on("shown.bs.modal",function(){
        var $modal_dialog = $("#lcProcessImageModalDialog");
        $modal_dialog.css({'margin': 0 + 'px auto'});
        $modal_dialog.css({'width':reGetBodyWidth()+'px'});
        // $modal_dialog.css({'width':reGetBodyWidth()*0.55+'px'});
        if(++ImageModalCount == 1) {
            //加载表单数据
            ajaxBRequestCallFn(workflowModules+'/lcProcess/image/base64/'+id,{},function(result){
                $('#imagePath').attr("src", result.data);
            });
        }
    });
}

/**
 * 拷贝流程
 * @param id
 */
function copyProcess(id){
    $('#defaultCopyProcessForm').bootstrapValidator({
        message:'此值不是有效的'
    });
    var copyProcessModalCount = 0 ;
    $('#copyProcessPanelBody').height(reGetBodyHeight()-218);
    $('#copyProcessModal').modal({backdrop:'static',keyboard:false});
    $('#copyProcessModal').modal({"backdrop":"static"}).modal('show').on("shown.bs.modal",function(){
        // $('#copyProcessModalDialog').modal('show').css({ width: reGetBodyWidth()*0.55+'px'});
        var $modal_dialog = $("#copyProcessModalDialog");
        $modal_dialog.css({'margin': 0 + 'px auto'});
        $modal_dialog.css({'width':reGetBodyWidth()+'px'});
        if(++copyProcessModalCount == 1) {
            //加载表单数据
            ajaxBRequestCallFn(workflowModules+'/lcProcess/get/'+id,{},function(result){
                $('#processId').val(result.data.lc_process_id);
                $('#lc_process_title').val(result.data.lc_process_title+"（复制）");
                $('#moduleKey').val(result.data.moduleKey+"-");
                $('#lc_product_idTemp').val(result.data.lc_product_id);
                $('#lc_group_idTemp').val(result.data.lc_group_id);
                //产品线下拉框数据
                initProductList("lc_product_id_");
                initGroupListSetV("lc_group_id_","lc_product_idTemp","lc_group_idTemp")
            });
        }
    });
}

/**
 * 复制
 */
function doCopy() {
    submitBFormCallFn('defaultCopyProcessForm',workflowModules+'/lcProcess/copy',function(result){
        try {
            if(typeof(result.success) != "undefined"){
                if(result.success){
                    window.parent.toastrBoot(3,result.message);
                    closeLcProcessWin();
                    $('#copyProcessModal').modal('hide');
                }else{
                    window.parent.toastrBoot(4,result.message);
                }
            }
        } catch (e) {
            window.parent.toastrBoot(4,"复制异常！");
        }
    },null,"POST");
}